from django.contrib import admin
from . import models as my_models


class ReceiveSharedAdmin(admin.ModelAdmin):
    list_display = ("id", "share_id", "user_id", "create_time")


class RandomCouponRecordAdmin(admin.ModelAdmin):
    list_display = ("id", "share_id", "user_id")


class UserShareAdmin(admin.ModelAdmin):
    list_display = ("id", "user_id", "create_time")


class BannerAdmin(admin.ModelAdmin):
    list_display = ("id", "name",)


class TradeAdmin(admin.ModelAdmin):
    list_display = ("id", "name", "key", "order",)


class CoffeeAdmin(admin.ModelAdmin):
    list_display = ("name", "price", "c_type")


class CoffeeOrdersAdmin(admin.ModelAdmin):
    list_display = (
        "id","goods_tag", "out_trade_no",
        "orderNo", "user_name", "goods_name", "payment", "cash", "price",
        "result_desc", "coffee_make_result", "coupon_code", "pay_time", "coffee_client_channel_name")
    search_fields = ('id', "orderNo", "coffee_client__channel__name")

    def coffee_client_channel_name(self, model):
        if model.coffee_client:
            if model.coffee_client.channel:
                return model.coffee_client.channel.name
            else:
                return "咖啡机没有渠道"
        else:
            return "订单没有机器"

    def user_name(self, model):
        if model.user.name:
            return model.user.name
        else:
            return model.user.mobile

    def coupon_code(self, model):
        if model.coupon:
            return model.coupon.code
        else:
            return "没有券"

    def result_desc(self, model):
        return model.get_result_display()

    def coffee_make_result_desc(self, model):
        return model.get_coffee_make_result_display()

    user_name.short_description = "用户"
    result_desc.short_description = "支付结果"
    coffee_make_result_desc.short_description = "制作结果"
    coupon_code.short_description = "券"
    coupon_code.coffee_client_channel_name = "订单机器渠道名称"


class CoffeeClientAdmin(admin.ModelAdmin):
    list_display = ("channel_name", "address", "sign", "title", "remark", "lon", "lat", "sort_level")

    def channel_name(self, model):
        return model.channel.name

    channel_name.short_description = "渠道"


class ChannelAdmin(admin.ModelAdmin):
    list_display = ("id", "name", "key",)


class CouponInfoAdmin(admin.ModelAdmin):
    list_display = ("id", "user_name", "name", "channel_name", "c_type_desc", "expired", "quota", "given", "status")
    search_fields = ("user__mobile",)

    def c_type_desc(self, model):
        return model.get_c_type_display()

    def channel_name(self, model):
        return model.channel.name

    def user_name(self, model):
        if model.user.name:
            return model.user.name
        else:
            return model.user.mobile

    def status_desc(self, model):
        return model.get_status_display()

    c_type_desc.short_description = "类型"
    channel_name.short_description = "渠道"
    user_name.short_description = "用户"
    status_desc.short_description = "状态"


class CouponMakeAdmin(admin.ModelAdmin):
    list_display = ("id", "channel_name", "c_type_desc", "create_time", "mobile")

    def channel_name(self, model):
        return model.channel.name

    def c_type_desc(self, model):
        return model.get_c_type_display()

    channel_name.short_description = "渠道"
    c_type_desc.short_description = "类型"
    search_fields = ("mobile",)


class GiveRecordAdmin(admin.ModelAdmin):
    list_display = (
        "id", "c_type_desc", "create_time", "status_desc", "own_user_mobile", "receive_user_mobile", "coupon")

    def c_type_desc(self, model):
        return model.get_c_type_display()

    def status_desc(self, model):
        return model.get_status_display()

    def own_user_mobile(self, model):
        return model.own_user.mobile

    def receive_user_mobile(self, model):
        if model.receive_user:
            return model.receive_user.mobile
        else:
            return "用户未领取(扫码)"

    c_type_desc.short_description = "类型"
    status_desc.short_description = "接收状态"
    own_user_mobile.short_description = "分享人"
    receive_user_mobile.short_description = "接收人"


class CardBagAdmin(admin.ModelAdmin):
    list_display = ("name", "bank", "expire", "price")


class CardBagRecordAdmin(admin.ModelAdmin):
    list_display = ("id", "desc_cardbag", "own_user", "receive_user", "status", "create_time", "update_time")

    def desc_cardbag(self, model):
        return model.cardbag.card_type

    desc_cardbag.short_description = "卡包"


class UserCardBagAdmin(admin.ModelAdmin):
    list_display = (
        "id", "user_id", "expire", "make_type", "bag_status", "giving", "refund_coupon_ids", "out_trade_no",
        "create_time")


class CardBagOrdersAdmin(admin.ModelAdmin):
    list_display = ("id", "result_desc",)

    def result_desc(self, model):
        return model.get_result_display()


class BankExchangeCodeAdmin(admin.ModelAdmin):
    list_display = ("id", "bank", "num", "code", "used", "expire_time")


class ReduceMoneyEveryDayAdmin(admin.ModelAdmin):
    list_display = ("id", "user_id", "money", "reduce_date")


class SysTypeAdmin(admin.ModelAdmin):
    list_display = ("id", "key", "name",)


class ReduceMoneyEveryDayActiveAdmin(admin.ModelAdmin):
    list_display = ("id", "user_id", "create_time",)


class CouponMakeEveryHourAdmin(admin.ModelAdmin):
    list_display = ("id", "channel_key", "name", "start_time", "end_time", "c_type_desc", "quota")
    search_fields = ("channel_key",)

    def result_desc(self, model):
        return model.get_result_display()

    def c_type_desc(self, model):
        return model.get_c_type_display()

    c_type_desc.short_description = "类型"


class GroupQrCodeAdmin(admin.ModelAdmin):
    list_display = ("id", "name", "show", "url")


class ShopInfoAdmin(admin.ModelAdmin):
    list_display = ("name", "lat", "lon", "addr")


class ShopInfoInline(admin.TabularInline):
    model = my_models.ShopInfo
    max_num = 20
    extra = 1  # 用于添加外键关系模型的空行
    verbose_name = '门店'  # 外键关系模型列表最下面的添加另外一个***,所用的名字
    show_change_link = True  # 显示修改连接,这个很有用啊!!!!
    can_delete = True  # 是否显示删除框


class InvoiceInfoInline(admin.TabularInline):
    model = my_models.InvoiceInfo
    max_num = 20
    extra = 1  # 用于添加外键关系模型的空行
    verbose_name = '发票信息'  # 外键关系模型列表最下面的添加另外一个***,所用的名字
    show_change_link = True  # 显示修改连接,这个很有用啊!!!!
    can_delete = True  # 是否显示删除框


class MakeH5Admin(admin.ModelAdmin):
    list_display = ("key", "title", "show", "expire_time", "active_time", "rules")
    inlines = (ShopInfoInline,)


class InvoiceRecordAdmin(admin.ModelAdmin):
    list_display = ("id", "username", "cash", "ready", "create_time", "full_name", "card_num", "email_addr")
    # inlines = (InvoiceInfoInline,)


class CodeQRAdmin(admin.ModelAdmin):
    list_display = ("key", "name", "c_type", "quota", "url")
    # inlines = (InvoiceInfoInline,)
    search_fields = ('key', "name",)


class LiftMobileAdmin(admin.ModelAdmin):
    list_display = ("id", "mobile")
    search_fields = ("mobile",)


class MemberAddrAdmin(admin.ModelAdmin):
    list_display = ("id", "sign", "level")

    def level_desc(self, model):
        return model.get_level_display()

    level_desc.short_description = "会员等级"


class MemberCenterAdmin(admin.ModelAdmin):
    list_display = ("id", "sign", "level", "mobile", "finished")

    def level_desc(self, model):
        return model.get_level_display()

    level_desc.short_description = "会员等级"


class ShareFriendAdmin(admin.ModelAdmin):
    list_display = ("share_id", "user_id", "status", "success_time",)

class BankDiscountAdmin(admin.ModelAdmin):
    list_display = ("id", "bank_code", "bank_name", "num", "pay_type")

class UserBankDiscountNumAdmin(admin.ModelAdmin):
    list_display = ("id", "create_year", "create_month", "num", "bank_code", "pay_type", "user_id")

class GoodsTagLevelAdmin(admin.ModelAdmin):
    list_display = ("id", "goods_tag", "num", "update_time")


class PayTitleAdmin(admin.ModelAdmin):
    list_display = ("id", "title", "client_id", 'pay_type', "show")

admin.site.register(my_models.InvoiceRecord, InvoiceRecordAdmin)
admin.site.register(my_models.PayTitle, PayTitleAdmin)
admin.site.register(my_models.GoodsTagLevel, GoodsTagLevelAdmin)
admin.site.register(my_models.SysType, SysTypeAdmin)
admin.site.register(my_models.MemberAddr, MemberAddrAdmin)
admin.site.register(my_models.MemberCenter, MemberCenterAdmin)
admin.site.register(my_models.MakeH5, MakeH5Admin)
admin.site.register(my_models.ShopInfo, ShopInfoAdmin)
admin.site.register(my_models.Coffee, CoffeeAdmin)
admin.site.register(my_models.Channel, ChannelAdmin)
admin.site.register(my_models.CoffeeClient, CoffeeClientAdmin)
admin.site.register(my_models.CouponInfo, CouponInfoAdmin)
admin.site.register(my_models.CouponMake, CouponMakeAdmin)
admin.site.register(my_models.GiveRecord, GiveRecordAdmin)
admin.site.register(my_models.CoffeeOrder, CoffeeOrdersAdmin)
admin.site.register(my_models.Banner, BannerAdmin)
admin.site.register(my_models.Trade, TradeAdmin)
admin.site.register(my_models.CardBag, CardBagAdmin)
admin.site.register(my_models.CardBagRecord, CardBagRecordAdmin)
admin.site.register(my_models.CardBagOrders, CardBagOrdersAdmin)
admin.site.register(my_models.UserCardBag, UserCardBagAdmin)
admin.site.register(my_models.BankExchangeCode, BankExchangeCodeAdmin)
admin.site.register(my_models.ReceiveShared, ReceiveSharedAdmin)
admin.site.register(my_models.UserShare, UserShareAdmin)
admin.site.register(my_models.RandomCouponRecord, RandomCouponRecordAdmin)
admin.site.register(my_models.ReduceMoneyEveryDay, ReduceMoneyEveryDayAdmin)
admin.site.register(my_models.ReduceMoneyEveryDayActive, ReduceMoneyEveryDayActiveAdmin)
admin.site.register(my_models.CouponMakeEveryHour, CouponMakeEveryHourAdmin)
admin.site.register(my_models.GroupQrCode, GroupQrCodeAdmin)
admin.site.register(my_models.CodeQR, CodeQRAdmin)
admin.site.register(my_models.LiftMobile, LiftMobileAdmin)
admin.site.register(my_models.ShareFriend, ShareFriendAdmin)
admin.site.register(my_models.BankDiscount, BankDiscountAdmin)
admin.site.register(my_models.UserBankDiscountNum, UserBankDiscountNumAdmin)
